package com.hqyj.erpserver.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.erpserver.annoation.Auth;
import com.hqyj.erpserver.dao.entity.Warehouse;
import com.hqyj.erpserver.service.IWarehouseService;
import com.hqyj.erpserver.util.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author HQYJ
 * @since 2021-10-21
 */
@RestController
@CrossOrigin//允许跨域请求
@Api(tags = "仓库管理")  //启用swagger文档
@RequestMapping("/warehouse")
public class WarehouseController {
    @Autowired
    private IWarehouseService warehouseService;
    Logger logger = LoggerFactory.getLogger(WarehouseController.class);

    @RequestMapping("/addWarehouse")
    @ApiOperation(value = "新增仓库记录")
    public RestResult<Warehouse> addWarehouse(Warehouse warehouse) {
        warehouseService.save(warehouse);
        return RestResult.ok("新增仓库成功");
    }

    @RequestMapping("/getWarehousePage")
    @ApiOperation(value = "分页查询仓库", notes = "按条件查询仓库信息，分页返回数据")//方法的接口文档描述
    @Auth(roles = "")
    public RestResult<Warehouse> getWarehousePage(
            @ApiParam(value = "分页的页码", required = true)//参数的接口文档描述
            @RequestParam(defaultValue = "1") Integer page,
            @ApiParam(value = "每页记录数", required = true)
            @RequestParam(defaultValue = "2") Integer rows,
            @ApiParam(value = "查询条件") Warehouse warehouse) {
        logger.info("分页查询仓库列表");
        logger.debug("生成Page对象："+page+";"+rows);
        Page<Warehouse> warehousePage = new Page<>(page, rows);
        QueryWrapper<Warehouse> warehouseQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isBlank(warehouse.getWhName())) {
            warehouseQueryWrapper.like("wh_name", warehouse.getWhName());
        }
        if (!StringUtils.isBlank(warehouse.getWhAddr())) {
            warehouseQueryWrapper.or().like("wh_addr", warehouse.getWhAddr());
        }
        logger.debug("调用service方法");
        Page<Warehouse> pageResult = warehouseService.page(warehousePage, warehouseQueryWrapper);
        logger.debug("返回RestResult对象");
        return RestResult.ok(pageResult.getRecords(), pageResult.getTotal());
    }


    @RequestMapping("/updateWarehouse")
    @ApiOperation(value = "修改仓库记录")
    public RestResult<Warehouse> updateWarehouse(@ApiParam(value = "修改项") Warehouse warehouse) {
        warehouseService.updateById(warehouse);
        return RestResult.ok("修改仓库成功");
    }

    @RequestMapping("/deleteWarehouse")
    @ApiOperation(value = "删除仓库记录", notes = "根据主键删除")
    public RestResult<Warehouse> deleteWarehouse(@ApiParam(value = "仓库编号") Integer whId) {
        warehouseService.removeById(whId);
        return RestResult.ok("删除成功");
    }
}

